
<?php echo $__env->make('admin::scripts.select', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

<script require="@select2" init="<?php echo $selector; ?>">
    var configs = <?php echo admin_javascript_json($configs); ?>;

    <?php echo $__env->yieldContent('admin.select-ajax'); ?>

    <?php if(isset($remoteOptions)): ?>
    $.ajax(<?php echo admin_javascript_json($remoteOptions); ?>).done(function(data) {
        configs.data = data;

        $this.each(function (_, select) {
            select = $(select);

            select.select2(configs);

            var value = select.data('value') + '';

            if (value) {
                select.val(value.split(',')).trigger("change")
            }
        });
    });
    <?php else: ?>
    $this.select2(configs);
    <?php endif; ?>

    <?php echo $cascadeScript; ?>

</script>

<?php if(isset($loads)): ?>
    
    <script once>
        var selector = '<?php echo $selector; ?>';

        var fields = '<?php echo $loads['fields']; ?>'.split('^');
        var urls = '<?php echo $loads['urls']; ?>'.split('^');

        var refreshOptions = function(url, target) {
            $.ajax(url).then(function(data) {
                target.find("option").remove();
                $(target).select2({
                    data: $.map(data, function (d) {
                        d.id = d.<?php echo e($loads['idField'], false); ?>;
                        d.text = d.<?php echo e($loads['textField'], false); ?>;
                        return d;
                    })
                }).val(target.data('value').split(',')).trigger('change');
            });
        };

        $(document).off('change', selector);
        $(document).on('change', selector, function () {
            var _this = this;
            var promises = [];

            fields.forEach(function(field, index){
                var target = $(_this).closest('.fields-group').find('.' + fields[index]);

                if (_this.value !== '0' && ! _this.value) {
                    return;
                }
                promises.push(refreshOptions(urls[index] + "?q="+ _this.value, target));
            });

            $.when(promises).then(function() {});
        });
        $(selector).trigger('change');
    </script>
<?php endif; ?>

<?php echo $__env->yieldContent('admin.select-load'); ?>


<?php echo $__env->yieldContent('admin.select-lang'); ?><?php /**PATH D:\phpstudy_pro\WWW\www.dcat.loc\vendor\dcat\laravel-admin\src/../resources/views/form/select-script.blade.php ENDPATH**/ ?>